package com.pingan.haofang.searchcloud.api.search;

import com.pingan.haofang.searchcloud.api.query.parser.exception.SqlParseException;
import com.pingan.haofang.searchcloud.api.search.form.EsNativeSearchForm;
import com.pingan.haofang.searchcloud.api.search.form.NativeSearchForm;
import com.pingan.haofang.searchcloud.api.search.form.SqlForm;
import com.pingan.haofang.searchcloud.api.search.validator.SqlFormValidator;
import com.pingan.haofang.searchcloud.common.constants.WebConstants;
import com.pingan.haofang.searchcloud.common.exception.SystemException;
import com.pingan.haofang.searchcloud.common.validator.annotation.Valid;
import com.pingan.haofang.searchcloud.index.dto.NativeResultDTO;
import com.pingan.haofang.searchcloud.index.dto.SqlResultDTO;
import com.pingan.haofang.searchcloud.index.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 搜索API
 *
 * @author zhangbi617
 * @date 2017/04/28
 */
@RestController
@RequestMapping(value = WebConstants.API_PREFIX + "/v1/sql")
@Api(description = "搜索API接口", tags = "Search.SearchController")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @Valid(SqlFormValidator.class)
    @ApiOperation("SQL查询GET接口")
    public SqlResultDTO select(SqlForm form) {
        try {
            return searchService.query(form.getSql());
        } catch (SystemException e) {
            throw e;
        } catch (SqlParseException e) {
            throw new SystemException("search.sql.illegal", new Object[]{e.getMessage()}, e);
        } catch (Exception e) {
            throw new SystemException(e.getMessage(), e);
        }
    }

    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @Valid(SqlFormValidator.class)
    @ApiOperation("SQL查询POST接口")
    public SqlResultDTO postSelect(@ApiParam(name = "payload", value = "payload")
                                   @RequestBody SqlForm sql) {
        return this.select(sql);
    }

    @RequestMapping(value = "/nativeSelect", method = RequestMethod.POST)
    @ApiOperation("原生查询")
    public NativeResultDTO postNativeSelect(@ApiParam(name = "payload", value = "payload")
                                            @RequestBody NativeSearchForm form) {
        return searchService.nativeQuery(form.getIndexName(), form.getParams());
    }

    @RequestMapping(value = "/esNativeSelect", method = RequestMethod.POST)
    @ApiOperation("es原生查询")
    public NativeResultDTO postEsNativeSelect(@ApiParam(name = "payload", value = "payload")
                                            @RequestBody EsNativeSearchForm form) throws Exception {
        return searchService.esNativeQuery(form.getIndexName(), form.getParams());
    }

}
